package {{packagePath}}.service.impl;

import {{packagePath}}.service.{{className}}Service;
import {{packagePath}}.mapper.{{className}}Mapper;
import {{packagePath}}.entity.{{className}};
import {{packagePath}}.dto.{{className}}ReqDTO;
import {{packagePath}}.vo.{{className}}RespVO;
import {{packagePath}}.util.{{className}}Assembler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@Service
public class {{className}}ServiceImpl extends ServiceImpl<{{className}}Mapper, {{className}}> implements I{{className}}Service {

    @Autowired
    private {{className}}Mapper baseMapper;

    @Override
    public {{className}}RespVO queryById(Long id) {
        if(id == null) {
            return null;
        }
        {{className}} entity = baseMapper.selectById(id);
        return {{className}}Assembler.entity2dto(entity);
    }

    @Override
    public PageResponse<{{className}}RespVO> page(PageRequest<{{className}}ReqDTO> request) {
        {{className}}ReqDTO condition = request.getQueryCondition();
        Page<{{className}}RespVO> page = new Page<>(request.getPageIndex(), request.getPageSize());
        IPage<{{className}}RespVO> pageData = baseMapper.pageQuery(page, condition);
        return new PageResponse<>((Page<?>)pageData, pageData.getRecords());
    }

    @Override
    public List<{{className}}RespVO> list({{className}}ReqDTO reqDto) {
        return baseMapper.queryList(reqDto);
    }

    @Override
    public void add({{className}}ReqDTO reqDto) {
        {{className}} entity = {{className}}Assembler.dto2entity(reqDto);
        baseMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchAdd(List<{{className}}ReqDTO> dtoList) {
        dtoList.forEach(this::add);
    }

    @Override
    public void update({{className}}ReqDTO reqDto) {
        {{className}} oldEntity = baseMapper.selectById(reqDto.getId());
        if (oldEntity == null) {
            throw new RecordNotFoundException();
        }
        {{className}} newEntity = {{className}}Assembler.dto2entity(reqDto, oldEntity);
        baseMapper.updateById(newEntity);
    }

    @Override
    public void delete(Long id) {
        baseMapper.deleteById(id);
    }

    @Override
    public void batchDelete(List<Long> ids) {
        baseMapper.deleteBatchIds(ids);
    }
}